package edu.gyc.hishiro.service.impl;

import edu.gyc.hishiro.model.Student;
import edu.gyc.hishiro.dao.StudentDao;
import edu.gyc.hishiro.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.CacheException;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ls
 * @since 2021-01-04
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "stus")
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
    @Override
    @CacheEvict(key = "#id")
    public void delete(Integer id) {
        log.info("delete student called...");
        this.removeById(id);

    }

    @Override
    @CachePut(key = "#stu.getId()")
    public Student update(Student stu) {
        log.info("update student called...");
        this.saveOrUpdate(stu);
        return stu;
    }

    @Override
    @Cacheable(key = "#id")
    public Student findById(Integer id) {
        log.info("findebyId student called......");
        Student stu = this.getById(id);
        return stu;
    }

    @Override
    @Cacheable(key="#stu.getId()")
    public Student saveStu(Student stu) {
        log.info("saveStu called....");
        this.save(stu);
        return stu;
    }
//    //这里的单引号不能少，否则会报错，被识别是一个对象
//    private static final String CACHE_KEY = "'stu'";
//    private static final String DEMO_CACHE_NAME = "stus";
//    @Override
//
//    @CacheEvict(value = DEMO_CACHE_NAME,key ="'stu_'+#id")
//    public void delete(Integer id) {
//        this.removeById(id);
//    }
//
//    @Override
//    @CachePut(value = DEMO_CACHE_NAME,key ="'stu_'+#student.getId()")
//    public boolean update(Student student) {
//
//        return this.saveOrUpdate(student);
//    }
//
//    @Override
//    @Cacheable(value = DEMO_CACHE_NAME,key ="'stu_'+#id")
//    public Student findById(Integer id) {
//        log.info("没有走缓存 "+id);
//        return this.getById(id);
//    }
//
//    @Override
//    @CacheEvict(value = DEMO_CACHE_NAME,key =CACHE_KEY)
//    public boolean saveSTu(Student student) {
//        return this.save(student);
//    }
}
